{include="header"}

<script type="text/javascript">
   var numlineas = 1;
   function recalcular()
   {
      var l_uds = 0;
      var l_pvp = 0;
      var l_iva = 0;
      var neto = 0;
      var total_iva = 0;
      
      for(var i=1; i<=numlineas; i++)
      {
         if($("#linea_"+i).length > 0)
         {
            l_uds = parseFloat( $("#cantidad_"+i).val() );
            l_pvp = parseFloat( $("#pvp_"+i).val() );
            l_iva = parseFloat( $("#iva_"+i).val() );
            $("#pvpt_"+i).val( l_uds*l_pvp*(100+l_iva)/100 );
            
            neto += l_uds*l_pvp;
            total_iva += l_uds*l_pvp*l_iva/100;
         }
      }
      
      neto = fs_round(neto, {#FS_NF0#});
      total_iva = fs_round(total_iva, {#FS_NF0#});
      $("#tpv_total").html( show_precio(neto+total_iva) );
      $("#tpv_total2").val( fs_round(neto+total_iva, {#FS_NF0#}) );
   }
   function linea_sum_ud(num,value)
   {
      var udl = parseInt($("#cantidad_"+num).val()) + parseInt(value);
      if(udl < 1)
         $("#linea_"+num).remove();
      else
         $("#cantidad_"+num).val(udl);
      recalcular();
   }
   function ajustar_pvpt(num)
   {
      if($("#linea_"+num).length > 0)
      {
         var l_uds = parseFloat( $("#cantidad_"+num).val() );
         var l_pvp = 0;
         var l_iva = parseFloat( $("#iva_"+num).val() );
         var l_pvpt = parseFloat( $("#pvpt_"+num).val() );
         var neto = 0;
         var total_iva = 0;
         
         if( !isNaN(l_pvpt) )
         {
            $("#pvp_"+num).val( 100*l_pvpt/(l_uds*(100+l_iva)) );
            
            for(var i=1; i<=numlineas; i++)
            {
               if($("#linea_"+i).length > 0)
               {
                  l_uds = parseFloat( $("#cantidad_"+i).val() );
                  l_pvp = parseFloat( $("#pvp_"+i).val() );
                  l_iva = parseFloat( $("#iva_"+i).val() );
                  
                  neto += l_uds*l_pvp;
                  total_iva += l_uds*l_pvp*l_iva/100;
               }
            }
            
            neto = fs_round(neto, {#FS_NF0#});
            total_iva = fs_round(total_iva, {#FS_NF0#});
            $("#tpv_total").html( show_precio(neto+total_iva) );
            $("#tpv_total2").val( fs_round(neto+total_iva, {#FS_NF0#}) );
            update_lcd(neto + total_iva);
         }
      }
   }
   function send_albaran()
   {
      if(document.f_albaran.codbar.value == '')
      {
         if(numlineas > 1)
         {
            save_modal();
         }
      }
      else
      {
         $.ajax({
            type: 'POST',
            url: '{$fsc->url()}',
            dataType: 'html',
            data: 'codbar2='+document.f_albaran.codbar.value+'&numlineas='+numlineas,
            success: function(datos) {
               if( datos.indexOf('<!--no_encontrado-->') != -1 )
               {
                  alert('¡Artículo no encontrado!');
                  document.f_albaran.codbar.value = '';
               }
               else
               {
                  $("#tpv_albaran").append(datos);
                  numlineas += 1;
                  document.f_albaran.codbar.value = '';
                  recalcular();
               }
            }
         });
      }
   }
   function save_modal()
   {
      $("#modal_guardar").modal('show');
      document.f_albaran.efectivo.select();
   }
   function save_albaran()
   {
      if(numlineas > 1)
      {
         document.f_albaran.total2.disabled = false;
         document.f_albaran.numlineas.value = numlineas;
         document.f_albaran.submit();
      }
      else
      {
         alert('No has vendido nada.');
         fs_hide_popups();
         document.f_albaran.codbar.focus();
      }
   }
   $(document).ready(function() {
      document.f_albaran.codbar.focus();
      $("#b_codbar").keypress(function(e) {
         if(e.which == 13)
         {
            e.preventDefault();
            send_albaran();
         }
      });
      $("#tpv_efectivo").keyup(function(e) {
         $("#tpv_cambio").val( number_format(parseFloat($(this).val()) - parseFloat($("#tpv_total2").val()),2,'.','') );
      });
      $('#tpv_efectivo').keypress(function(e) {
         if(e.which == 13)
            save_albaran();
      });
   });
</script>

<form action="{$fsc->url()}" name="f_albaran" method="post" class="form">
   <input type="hidden" name="petition_id" value="{$fsc->random_string()}"/>
   <input type="hidden" name="numlineas" value="0"/>
   <div class="container-fluid">
      <div class="row" style="margin-top: 10px; margin-bottom: 10px;">
         <div class="col-md-8">
            Cliente: <a href="{$fsc->cliente->url()}">{$fsc->cliente->nombre}</a> |
            Gastados: {$fsc->show_precio($fsc->clan->gastado())} de {$fsc->show_precio($fsc->clan->limite)}
         </div>
         <div class="col-md-4 text-right">
            <div id="tpv_total" style="font-size: 30px;">{$fsc->show_precio(0)}</div>
         </div>
      </div>
   </div>
   
   <div class="table-responsive">
      <table class="table table-hover">
         <thead>
            <tr>
               <th class="text-left">Referencia</th>
               <th class="text-left">Descripción</th>
               <th class="text-right">Disponible</th>
               <th></th>
               <th class="text-right">Unidades o Kilos</th>
               <th></th>
               <th class="text-right">TOTAL+IVA</th>
            </tr>
         </thead>
         <tbody id="tpv_albaran"></tbody>
      </table>
   </div>
   
   <div class="container-fluid">
      <div class="row">
         <div class="col-sm-12">
            <input id="b_codbar" class="form-control" type="text" name="codbar" autocomplete="off" placeholder="Introduce el código de barras"/>
            <br/><br/>
         </div>
      </div>
      <div class="row">
         <div class="col-sm-6">
            <a class="btn btn-lg btn-danger" href="index.php?page=tpv_supermercado">
               <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> &nbsp; Cancelar
            </a>
         </div>
         <div class="col-sm-6 text-right">
            <button class="btn btn-lg btn-primary" type="button" onclick="save_modal();">
               <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar
            </button>
         </div>
      </div>
   </div>
   
   <div class="modal" id="modal_guardar">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
               <h4 class="modal-title">Guardar</h4>
            </div>
            <div class="modal-body">
               <div class="form-group">
                  Total:
                  <input class="form-control" type="text" name="total2" value="0.00" id="tpv_total2" disabled="disabled"/>
               </div>
               <div class="form-group">
                  Efectivo:
                  <input class="form-control" type="text" name="efectivo" value="0.00" id="tpv_efectivo" autocomplete="off"/>
               </div>
               <div class="form-group">
                  Cambio:
                  <input class="form-control" type="text" name="cambio" value="0.00" id="tpv_cambio" autocomplete="off"/>
               </div>
               <div class="form-group">
                  Observaciones:
                  <textarea name="observaciones" class="form-control"></textarea>
               </div>
            </div>
            <div class="modal-footer">
               <button class="btn btn-lg btn-danger pull-left" type="button" onclick="fs_hide_popups();">
                  <span class="glyphicon glyphicon-remove"></span> &nbsp; Cancelar
               </button>
               <button class="btn btn-lg btn-primary" type="button" onclick="save_albaran();">
                  <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar
               </button>
            </div>
         </div>
      </div>
   </div>
</form>

<div class="hidden">
   <iframe src="http://localhost:10080" height="0"></iframe>
</div>

{include="footer"}
